package com.datastructure.test.addopt;

import java.util.ArrayList;
import java.util.List;

public class AddOpt {

    int n,t;
    String num;
    List<String> ans = new ArrayList<>();
    public String[] addOpt (String num, int target) {
        // write code here
        n = num.length();
        t = target;
        this.num = num;
        dfs(0,0,0,"");
        return ans.toArray(new String[0]);
    }

    public void dfs(int index,long prev,long cur,String s){
        if(index==n){
            if(cur==t){
                ans.add(s);
            }
            return;
        }
        for(int i=index;i<n;i++){
            if(i!=index&&num.charAt(index)==0)break;
            long next = Long.parseLong(num.substring(index,i+1));
            if(index==0){
                dfs(i+1,next,next,""+next);
            }else{
                dfs(i+1,next,cur+next,s+"+"+next);
                dfs(i+1,-next,cur-next,s+"-"+next);
                long x=prev*next;
                dfs(i+1,x,cur-prev+x,s+"*"+next);
            }
        }
    }
}
